using UnityEngine;
using UnityEngine.UI;
using EchoFramework;

public class SettingView : UIViewBase
{
    private Button closeBtn;
    private Slider musicSlider;
    private Slider soundSlider;

    public override void OnAwake()
    {
        base.OnAwake();
        closeBtn = prefabBinder.Find("CloseBtn").GetComponent<Button>();
        closeBtn.onClick.AddListener(OnClickCloseBtn);
        musicSlider = prefabBinder.Find("MusicSlider").GetComponent<Slider>();
        musicSlider.onValueChanged.AddListener(OnMusicSliderChange);
        soundSlider = prefabBinder.Find("SoundSlider").GetComponent<Slider>();
        soundSlider.onValueChanged.AddListener(OnSoundSliderChange);
    }

    public override void OnStart(params object[] objs)
    {
        base.OnStart(objs);
        float bgmVolume = PlayerPrefs.GetFloat("BgmVolume", 0.2f);
        float soundVolume = PlayerPrefs.GetFloat("SoundVolume", 0.2f);
        musicSlider.value = bgmVolume;
        soundSlider.value = soundVolume;
    }

    /// <summary>
    /// 点击关闭按钮
    /// </summary>
    private void OnClickCloseBtn()
    {
        AudioManager.Instance.PlayNormalBtnClip();
        AudioManager.Instance.SaveMusciSetting();
        CloseWindow();
    }
    
    /// <summary>
    /// 背景音乐改变
    /// </summary>
    /// <param name="changevalue">参数</param>
    private void OnMusicSliderChange(float changevalue)
    {
        AudioManager.Instance.SetBgmVolume(changevalue);
    }

    /// <summary>
    /// 音效改变
    /// </summary>
    /// <param name="changeValue">参数</param>
    private void OnSoundSliderChange(float changeValue)
    {
        AudioManager.Instance.SetSoundVolume(changeValue);
    }
}